<?php

namespace app\api\model;

use app\common\enums\StatusEnum;

class OauthOrderClass extends \app\common\model\OauthOrderClass
{
    /**
     * 判断指定班级是否已经购买
     * @param $oauthId
     * @param $classId
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOneByClass($oauthId, $classId)
    {
        return static::field('oc.id')
            ->alias('oc')
            ->join('lang_class lc', 'oc.class_id = lc.id')
            ->where([
                'oc.oauth_id' => $oauthId,
                'oc.class_id' => $classId,
                'lc.status' => StatusEnum::ENABLED
            ])->find();
    }

    /**
     * 我的课程
     * @param $oauthId
     * @param int $page
     * @param int $size
     * @return array
     * @throws \think\Exception\DbException
     */
    public static function myClasses($oauthId, $page, $size)
    {
        $offset = ($page - 1) * $size;

        $sql = static::field('oc.class_id AS classId, lc.type, l.name, l.icon,l.id AS langId, oc.money, oc.create_time AS createTime')
            ->alias('oc')
            ->join('lang_class lc', 'oc.class_id = lc.id')
            ->join('lang l', 'lc.lang_id = l.id')
            ->where(['oc.oauth_id' => $oauthId, 'lc.status' => StatusEnum::ENABLED]);

        // 返回班级ID、班级类型、语种名称、购买时间
        $model = $sql->order(['oc.create_time' => 'desc'])
            ->limit($offset, $size)
            ->select()
            ->toArray();

        foreach ($model as $key => $value) {
            $model[$key]['icon'] = config('file_prefix') . $value['icon'];
            $model[$key]['type'] = LangClass::attributes('type', $value['type']);
            $model[$key]['createTime'] = date('Y-m-d H:i:s', $value['createTime']);
        }
        return [
            'list' => $model,
            'count' => $sql->count()
        ];
    }

    /**
     * 我的课程 - 三级联动  语种名称+班级类型---课程类型---第几节课
     * @param $oauthId
     * @param $reciteType
     * @return array
     * @throws \think\Exception\DbException
     */
    public static function selectClasses($oauthId, $reciteType)
    {
        // 班级id、班级类型（初级班等）、语种名称
        $model = static::field('lc.id AS classId, lc.type, l.name,l.icon')
            ->alias('oc')
            ->join('lang_class lc', 'oc.class_id = lc.id')
            ->join('lang l', 'lc.lang_id = l.id')
            ->where(['oc.oauth_id' => $oauthId, 'lc.status' => StatusEnum::ENABLED])
            ->select()
            ->toArray();

        $result = [];
        foreach ($model as $key => $value) {
            $result[$key]['classId'] = $value['classId'];
            // 班级类型（初级班等）
            $type = LangClass::attributes('type', $value['type']);
            $result[$key]['className'] = $value['name'] . $type;
            $result[$key]['courses'] = LangClass::attributes('course', $value['type']);
            $result[$key]['icon'] = config('file_prefix') .$value['icon'];
        }

        foreach ($result as $key => $value) {
            foreach ($value['courses'] as $k => $v) {
                $result[$key]['courses'][$k]['typeArr'] = LangClassReciteType::getType($value['classId'], $v['type'], $reciteType);
            }
        }

        return $result;
    }
}
